﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceProcess;
using Microsoft.Win32;

namespace Sinacor.Infra.Tools.ErrorLogCollector
{
    public class EnvironmentCollector
    {
        public ClientEnvironmentData CollectClientEnvironment()
        {
            return this.CollectClientEnvironment(new ClientEnvironmentData());
        }

        private ClientEnvironmentData CollectClientEnvironment(ClientEnvironmentData environmentData)
        {

            environmentData.OperationalSystem = Environment.OSVersion.VersionString;
            environmentData.Cpu = (string)Microsoft.Win32.Registry.GetValue(@"HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor\0", "ProcessorNameString", string.Empty);
            environmentData.Memory = (long)new Microsoft.VisualBasic.Devices.ComputerInfo().TotalPhysicalMemory / 1048576;

            environmentData.User = Environment.UserDomainName + @"\" + Environment.UserName;
            environmentData.Computer = Environment.MachineName;

            return environmentData;
        }

        public ServerEnvironmentData CollectServerEnvironment()
        {
            ServerEnvironmentData environmentData = (ServerEnvironmentData)this.CollectClientEnvironment(new ServerEnvironmentData());

            environmentData.DbVersion = this.GetOracleVersions();

            environmentData.HosterServiceState = this.GetServiceProcessState("PSEServiceHoster");
            environmentData.ProcessManagerServiceState = this.GetServiceProcessState("Process Manager");
            environmentData.SinacorLoggingServiceState = this.GetServiceProcessState("SinacorLoggingService");
            environmentData.FixServiceState = this.GetServiceProcessState("BMFBOVESPA Fix Communication");
            environmentData.SinacorStsServiceState = this.GetServiceProcessState("STS Hoster Service");

            return environmentData;
        }

        private ServiceProcessState GetServiceProcessState(string serviceName)
        {
            ServiceController service = ServiceController.GetServices().FirstOrDefault(s => s.ServiceName == serviceName);

            if (service != null)
                return (ServiceProcessState)service.Status;
            else
                return ServiceProcessState.NotInstalled;
        }

        private string GetOracleVersions()
        {
            string version = string.Empty;

            RegistryKey rgkLM = Registry.LocalMachine;

            RegistryKey rgkAllHome = rgkLM.OpenSubKey(@"SOFTWARE\ORACLE");
            if (rgkAllHome != null)
            {
                foreach (string subkey in rgkAllHome.GetSubKeyNames())
                {
                    if (subkey.StartsWith("KEY_"))
                    {
                        if (!string.IsNullOrEmpty(version))
                            version += ", ";

                        version += subkey.Replace("KEY_", "");
                    }
                }
            }

            return version;
        }

    }
}
